#------>第一阶段
FROM ubuntu:18.04 as python-base

ARG PYTHON_PKG=Python-3.9.2.tgz
WORKDIR /tmp

#内网专用需配置代理
ENV http_proxy="proxyValue"
ENV https_proxy="proxyValue"
ENV ftp_proxy="proxyValue"

#内网专用，使用华为源
#<aptOri>RUN sed -i "s@http://archive.ubuntu.com@aptValue@g" /etc/apt/sources.list && \
#<aptOri>    sed -i "s@http://security.ubuntu.com@aptValue@g" /etc/apt/sources.list && \
#<aptOri>    sed -i "s@http://ports.ubuntu.com@aptValue@g" /etc/apt/sources.list

# 系统包
RUN apt update && apt-get install --no-install-recommends gcc g++ make cmake zlib1g zlib1g-dev xz-utils liblzma-dev\
    libbz2-dev libsqlite3-dev libssl-dev libxslt1-dev libffi-dev wget unzip zip pciutils net-tools vim -y

# compile and install python3.9.2
COPY $PYTHON_PKG .
RUN tar -zxvf $PYTHON_PKG && cd Python-3.9.2 && ./configure --prefix=/usr/local/python3.9.2 --enable-shared && \
    make -j8 && make install && \
    cp /usr/local/python3.9.2/lib/libpython3.9.so.1.0 /usr/lib && \
    ln -sf /usr/local/python3.9.2/bin/python3 /usr/bin/python && \
    ln -sf /usr/local/python3.9.2/bin/python3 /usr/bin/python3 && \
    ln -sf /usr/local/python3.9.2/bin/python3 /usr/bin/python3.9 && \
    ln -sf /usr/local/python3.9.2/bin/python3 /usr/bin/python3.9.2 && \
    ln -sf /usr/local/python3.9.2/bin/pip3 /usr/bin/pip3.9 && \
    ln -sf /usr/local/python3.9.2/bin/pip3 /usr/bin/pip3.9.2 && \
    ln -sf /usr/local/python3.9.2/bin/pip3 /usr/local/python3.9.2/bin/pip3.9.2 && \
    ln -sf /usr/local/python3.9.2/bin/python3 /usr/local/python3.9.2/bin/python3.9.2

# 配置python环境变量
ENV LD_LIBRARY_PATH=/usr/local/python3.9.2/lib:$LD_LIBRARY_PATH
ENV PATH=/usr/local/python3.9.2/bin:$PATH

# pip3.9 内外网均使用豆瓣源，速度比较快
RUN cd /tmp && \
    apt-get download python3-distutils && \
    dpkg-deb -x python3-distutils_*.deb / && \
    rm python3-distutils_*.deb
COPY get-pip.py /tmp
#    wget -O get-pip.py https://bootstrap.pypa.io/get-pip.py --no-check-certificate
RUN python3.9 get-pip.py pipValue && \
    rm get-pip.py

RUN apt-get --purge remove gcc make cmake -y && \
    apt-get autoclean -y && \
    apt-get clean -y && \
    apt-get autoremove -y && \
    rm -rf /var/lib/apt/lists/*

RUN rm $PYTHON_PKG && rm -rf Python-3.9.2


#----->第二阶段
FROM python-base

ARG HOST_ASCEND_BASE=/usr/local/Ascend
ARG NNAE_PATH=/usr/local/Ascend/nnae/latest
ARG POSTBUILD_SH=postbuild.sh
WORKDIR /tmp

RUN mkdir -p /home/ascend/mindspore && \
    mkdir -p /usr/local/Ascend && \
    chmod 755 /usr/local/Ascend && \
    mkdir -p /usr/local/Ascend/driver/
COPY ascend_install.info /etc/
COPY version.info /usr/local/Ascend/driver
COPY model_zoo.tgz .
RUN tar -zxf model_zoo.tgz && \
    mv model_zoo /home/ascend/mindspore && \
    rm model_zoo.tgz

# 安装依赖
RUN apt-get update && apt install -y --no-install-recommends \
    dos2unix vim sudo openssl libnuma1 curl pkg-config \
    libblas3 liblapack3 liblapack-dev libblas-dev \
    gfortran libhdf5-dev libicu60 libxml2 libgl1-mesa-glx libgmpxx4ldbl openssh-server

# 配置shh
RUN echo "PermitRootLogin yes" >> /etc/ssh/sshd_config

# HwHiAiUser, hwMindX
RUN useradd -d /home/hwMindX -u 9000 -m -s /bin/bash hwMindX && \
    useradd -d /home/HwHiAiUser -u 1000 -m -s /bin/bash HwHiAiUser && \
    usermod -a -G HwHiAiUser hwMindX

# python包 外网使用豆瓣源，内网只能用华为源才能下到所有库
COPY pip_list.txt .

#外网用豆瓣源，内网不能用豆瓣源就用华为源
RUN pip3.9.2 install -r pip_list.txt pipValue
RUN pip3.9.2 install pycocotools pipValue
RUN pip3.9.2 install pytest-runner pipValue
RUN pip3.9.2 install mmcv==1.2.7 pipValue

#外网专用，pip使用豆瓣源
#<ex>RUN pip3.9.2 install -r pip_list.txt --trusted-host pypi.doubanio.com -i https://pypi.doubanio.com/simple
#<ex>RUN pip3.9.2 install pycocotools --trusted-host pypi.doubanio.com -i https://pypi.doubanio.com/simple
#<ex>RUN pip3.9.2 install pytest-runner --trusted-host pypi.doubanio.com -i https://pypi.doubanio.com/simple
#<ex>RUN pip3.9.2 install mmcv==1.2.7 --trusted-host pypi.doubanio.com -i https://pypi.doubanio.com/simple

RUN rm pip_list.txt

# Ascend包
RUN bash $INSTALL_ASCEND_PKGS_SH
COPY Ascend-cann-nnae*.run .
COPY toolkit/run_package/CANN-compiler-*.run .
COPY toolkit/run_package/CANN-runtime-*.run .
RUN ./Ascend-cann-nnae*.run --install-path=/usr/local/Ascend/ --install --quiet && \
    ./CANN-compiler-*.run --quiet --full --install-for-all && \
    ./CANN-runtime-*.run --quiet --full --install-for-all && \
    rm Ascend-cann-nnae*.run CANN-compiler-*.run CANN-runtime-*.run

# 建立atc工具软连接
RUN ln -s /usr/local/Ascend/latest/atc /usr/local/Ascend/atc

# mindspore安装
COPY mindspore*.whl .
RUN pip3.9.2 install mindspore*.whl && \
    pip3.9.2 install $NNAE_PATH/fwkacllib/lib64/te-*.whl && \
    pip3.9.2 install $NNAE_PATH/fwkacllib/lib64/topi-*.whl && \
    pip3.9.2 install $NNAE_PATH/fwkacllib/lib64/hccl-*.whl

# 清理软件包和pip缓存
RUN apt-get autoclean -y && \
    apt-get clean -y && \
    apt-get autoremove -y && \
    rm -rf /var/lib/apt/lists/* && \
    pip3.9.2 cache purge

#清除代理
ENV http_proxy=
ENV https_proxy=
ENV ftp_proxy=

#未兼容打补丁
RUN ln -s /usr/local/python3.9.2 /usr/local/python3.7.5 && \
    ln -s /usr/bin/python3.9.2 /usr/bin/python3 && \
    ln -s /usr/bin/python /usr/bin/python3.7 && \
    ln -s /usr/bin/python /usr/bin/python3.7.5

# 环境变量
ARG HOST_ASCEND_BASE=/usr/local/Ascend
ARG NNAE_PATH=/usr/local/Ascend/nnae/latest
RUN chmod 4755 /usr/bin/sudo
ENV GLOG_v=2
ENV MINDSPORE_CODE_PATH=/home/ascend/mindspore/model_zoo/official/cv
ENV TBE_IMPL_PATH=$NNAE_PATH/opp/op_impl/built-in/ai_core/tbe
ENV FWK_PYTHON_PATH=$NNAE_PATH/fwkacllib/python/site-packages
ENV PATH=$NNAE_PATH/fwkacllib/ccec_compiler/bin/:$HOST_ASCEND_BASE/atc/ccec_compiler/bin:$HOST_ASCEND_BASE/atc/bin:$PATH
ENV ASCEND_OPP_PATH=$NNAE_PATH/opp
ENV PYTHONPATH=$FWK_PYTHON_PATH:$FWK_PYTHON_PATH/auto_tune.egg:$FWK_PYTHON_PATH/schedule_search.egg:$TBE_IMPL_PATH:\
$HOST_ASCEND_BASE/atc/python/site-packages:$HOST_ASCEND_BASE/atc/python/site-packages/auto_tune.egg:\
$HOST_ASCEND_BASE/atc/python/site-packages/schedule_search.egg:$PYTHONPATH
ENV LD_LIBRARY_PATH=$NNAE_PATH/fwkacllib/lib64:/usr/local/Ascend/driver/lib64/common/:/usr/local/Ascend/driver/lib64/driver/:\
/usr/local/Ascend/add-ons/:/usr/local/Ascend/driver/tools/hccn_tool/:$HOST_ASCEND_BASE/atc/lib64:$LD_LIBRARY_PATH
      
